<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace app\exception;

class BaseError extends \Exception
{
    protected $previous;

    public function __construct()
    {
        $argsMap = func_get_args();
        $previous = $this->setPrevious($argsMap);
        $code = $this->setCode($argsMap);
        $message = $this->setMessage($argsMap);
        parent::__construct($message, $code, $previous);
    }

    protected function setPrevious(&$argsMap)
    {
        if (end($argsMap) instanceof Throwable) {
            $this->previous = array_pop($argsMap);
        }
    }

    protected function setCode(&$argsMap)
    {
        if (count($argsMap) >= 2 && is_integer(end($argsMap)) && strpos($argsMap[0], '%d') === false) {
            return array_pop($argsMap);
        }
        return 0;
    }

    protected function setMessage(&$argsMap)
    {
        return call_user_func_array('sprintf', $argsMap);
    }
}
